#include <stdio.h>  
#include <string.h>  
  
struct People {  
    char name[20];  
    int money, getMoney, leftMoney;  
} people[15];  
  
int main() {  
    int n;  
    if (scanf("%d", &n));  
    getchar();  
    for (int i = 0; i < n; i++)  
        gets(people[i].name);  
  
    for (int i = 0; i < n; i++) {  
        char temp[21];  
        gets(temp);  
        int j;  
        for (j = 0; j < n; j++)  
            if (strcmp(temp, people[j].name) == 0)  
                break;  
        int cnt, money;  
        if (scanf("%d%d", &people[j].money, &cnt));  
        getchar();  
        if (cnt != 0)  
            money = people[j].money / cnt;  
        people[j].leftMoney = people[j].money - money * cnt;  
        while (cnt-- != 0) {  
            gets(temp);  
            for (int k = 0; k < n; k++)  
                if (strcmp(temp, people[k].name) == 0)  
                    people[k].getMoney += money;  
        }  
    }  
    for (int i = 0; i < n; i++)  
        printf("%s %d\n", people[i].name, people[i].getMoney + people[i].leftMoney - people[i].money);  
    return 0;  
}  